package com.yiren.backstage.communication.utils;

/**
 * Time:2021/12/24 15:17
 * Author:xiaozan
 * Description:
 */
public class BytesUtils {
    /**
     * 将int转成byte数组
     * @param num
     * @return
     */
    public static byte[] int2bytes(int num) {
        byte[] result = new byte[8];
        byte[] bytes = Integer.toBinaryString(num).getBytes();
        if (bytes.length == 7) {
            result[0] = 0;
        } else if (bytes.length == 6) {
            result[0] = 0;
            result[1] = 0;
        } else if (bytes.length == 5) {
            result[0] = 0;
            result[1] = 0;
            result[2] = 0;
        } else if (bytes.length == 4) {
            result[0] = 0;
            result[1] = 0;
            result[2] = 0;
            result[3] = 0;
        } else if (bytes.length == 3) {
            result[0] = 0;
            result[1] = 0;
            result[2] = 0;
            result[3] = 0;
            result[4] = 0;
        } else if (bytes.length == 2) {
            result[0] = 0;
            result[1] = 0;
            result[2] = 0;
            result[3] = 0;
            result[4] = 0;
            result[5] = 0;
        } else if (bytes.length == 1) {
            result[0] = 0;
            result[1] = 0;
            result[2] = 0;
            result[3] = 0;
            result[4] = 0;
            result[5] = 0;
            result[6] = 0;
        }

        for (int i = 0; i < bytes.length; i++) {
            if (bytes.length == 8) {
                result[i] = (byte) (bytes[i] == 49 ? 1 : 0);
            } else if (bytes.length == 7) {
                result[i + 1] = (byte) (bytes[i] == 49 ? 1 : 0);
            } else if (bytes.length == 6) {
                result[i + 2] = (byte) (bytes[i] == 49 ? 1 : 0);
            } else if (bytes.length == 5) {
                result[i + 3] = (byte) (bytes[i] == 49 ? 1 : 0);
            } else if (bytes.length == 4) {
                result[i + 4] = (byte) (bytes[i] == 49 ? 1 : 0);
            } else if (bytes.length == 3) {
                result[i + 5] = (byte) (bytes[i] == 49 ? 1 : 0);
            } else if (bytes.length == 2) {
                result[i + 6] = (byte) (bytes[i] == 49 ? 1 : 0);
            } else if (bytes.length == 1) {
                result[i + 7] = (byte) (bytes[i] == 49 ? 1 : 0);
            }
        }
        return result;
    }

    public static String byteToString(byte[] bytes, int offset, int byteCount, String reg) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < byteCount; i++) {
            int h = (bytes[offset + i] >>> 4) & 0xF;
            int l = bytes[offset + i] & 0xF;
            char ch = (char) ((h < 10) ? ('0' + h) : ('A' + h - 10));
            char cl = (char) ((l < 10) ? ('0' + l) : ('A' + l - 10));
            sb.append(ch);
            sb.append(cl);
            if (reg != null && reg.length() > 0 && i != (byteCount - 1)) {
                sb.append(reg);
            }
        }
        return sb.toString();
    }
}
